package edu.zhku.ttms.view.panel.content;

import edu.zhku.ttms.service.UserService;
import edu.zhku.ttms.service.impl.UserServiceImpl;
import edu.zhku.ttms.utils.CheckUtil;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.font.LeftLabelFont;

import javax.swing.*;
import java.awt.*;

/**
 * @author Feng
 * @since 2021/4/23 20:20
 */

public class ModPwdPanel extends ViewPanel {

    static UserService userService = new UserServiceImpl();

    public ModPwdPanel() {
        
        super(true);

        Font font = new LeftLabelFont();

        JLabel originalPwdLabel = new JLabel("原密码");
        originalPwdLabel.setFont(font);
        originalPwdLabel.setBounds(260, 160, 132, 30);
        this.add(originalPwdLabel);

        JPasswordField originalPwdField = new JPasswordField();
        originalPwdField.setFont(font);
        originalPwdField.setBounds(392, 160, 308, 30);
        this.add(originalPwdField);

        JLabel newPwdLabel = new JLabel("新密码");
        newPwdLabel.setFont(font);
        newPwdLabel.setBounds(260, 215, 132, 30);
        this.add(newPwdLabel);

        JPasswordField newPwdField = new JPasswordField();
        newPwdField.setBounds(392, 215, 308, 30);
        this.add(newPwdField);

        JLabel reNewLabel = new JLabel("确认新密码");
        reNewLabel.setFont(font);
        reNewLabel.setBounds(260, 270, 132, 30);
        this.add(reNewLabel);

        JPasswordField reNewPwdField = new JPasswordField();
        reNewPwdField.setBounds(392, 270, 308, 30);
        this.add(reNewPwdField);

        JLabel tip = new JLabel();
        tip.setForeground(Color.RED);
        tip.setFont(font);
        tip.setBounds(392, 325, 440, 30);
        this.add(tip);

        JButton goBackButton = new JButton("回到首页");
        goBackButton.addActionListener(e -> ViewController.changeView(new SuccessPanel()));
        goBackButton.setBounds(392, 380, 148, 48);
        this.add(goBackButton);

        JButton modPwdButton = new JButton("修改密码");
        modPwdButton.addActionListener(e -> {
            String originalPwd = originalPwdField.getText();
            String newPwd = newPwdField.getText();
            String reNewPwd = reNewPwdField.getText();

            String modRes = userService.modifyPassword(originalPwd, newPwd, reNewPwd);

            if (CheckUtil.isSuccess(modRes)) {
                tip.setText("");
                JOptionPane.showMessageDialog(this, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                ViewController.changeView(new LoginPanel());
            } else {
                tip.setText(modRes);
            }
        });
        modPwdButton.setBounds(546, 380, 148, 48);
        this.add(modPwdButton);
    }

}
